<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
   <video id="video" width="600px" controls></video>
   <button id="start">开始</button>
   <script>
    let btn = document.getElementById("start");
    let video = document.getElementById("video");

    btn.addEventListener("click",async function(){
      let stream = await  navigator.mediaDevices.getDisplayMedia({
        video: true
      })

      //判断浏览器支持的类型
      const type = MediaRecorder.isTypeSupported("video/webm; codecs=vp9")?"video/webm; codecs=vp9":"video/webm"
      console.log(MediaRecorder.isTypeSupported("video/webm; codecs=vp9"),1);
      let mediaRecorder = new MediaRecorder(stream,{
        mimeType:type
      })
      let chunk = []
      mediaRecorder.addEventListener('dataavailable', function (e){
        chunk.push(e.data)
        console.log(chunk,2);
      })
      mediaRecorder.addEventListener('stop', function (){
        let blob = new Blob(chunk,{
          type:chunk[0].type
        })
        console.log(blob,3);
        let url = URL.createObjectURL(blob)
        console.log(url,4);
        video.src = url
        let a = document.createElement("a")
        a.href = url 
        a.download = '录像' + new Date().getTime()
        a.click()
      })

      mediaRecorder.start()

    })
   </script>
</body>
</html>